from typing import Optional, List

from sqlalchemy import or_
from sqlalchemy.orm import Session

from backend_module.jsonvo.reviews_manage_vo import BackendReviewsListQueryModel
from apis_module.model.reviews_model import ReviewsModel


class ReviewsManageDAO:
    @classmethod
    def get_reviews(cls, db: Session, query_model: BackendReviewsListQueryModel) -> Optional[List]:
        """
        后台管理商的点评列表查询方法
        :param db: ORM
        :param query_model: 查询参数集
        :return: 不分页列表
        """
        if query_model.keyword != '':
            like_condition = or_(ReviewsModel.reviews_title.like('%' + query_model.keyword + '%'),
                                 ReviewsModel.reviews_content.like('%' + query_model.keyword + '%'))
            reviews_list = db.query(ReviewsModel).filter(like_condition).filter(ReviewsModel.del_flag == False) \
                .filter(ReviewsModel.reviews_author == query_model.author if query_model.author else True) \
                .filter(ReviewsModel.reviews_group_code == query_model.groupCode if query_model.groupCode else True) \
                .order_by(ReviewsModel.release_time.desc()).all()
        else:
            reviews_list = db.query(ReviewsModel).filter(ReviewsModel.del_flag == False) \
                .filter(ReviewsModel.reviews_author == query_model.author if query_model.author else True) \
                .filter(ReviewsModel.reviews_group_code == query_model.groupCode if query_model.groupCode else True) \
                .order_by(ReviewsModel.release_time.desc()).all()

        return reviews_list
